package com.kerry.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;

import java.net.InetSocketAddress;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class UDPServer {
	private ChannelType channelType = ChannelType.NIO;
	private  InetSocketAddress localAddress = new InetSocketAddress(8888);
	
	private Channel acceptorChannel;//

	@Autowired
	ServerUDPHandler udpHandler;
	
	public UDPServer() {
	}

	public UDPServer(ChannelType channelType, int port) {
		this.channelType = channelType;
		this.localAddress = new InetSocketAddress(port);
	}

	public void start() {
		acceptorChannel = ServerUDPChannelFactory.createAcceptorChannel(
				channelType, localAddress, udpHandler);
	}

	public void stop() {
		if (acceptorChannel != null)
			acceptorChannel.close().addListener(ChannelFutureListener.CLOSE);
	}

	public void restart() {
		stop();
		start();

	}
}
